<template>
  <div class="app-container">
    <div>
      <el-button style="margin-left: 50px" @click="toPlateAdd" icon="el-icon-plus" type="primary" plain>新增</el-button>

    </div>

    <br />
    <el-table
      v-loading="listLoading"
      :data="list"
      show-header
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="plateId" width="95">
        <template slot-scope="scope">
          <!-- {{ scope.$index }} -->
          {{ scope.row.plateId }}
        </template>
      </el-table-column>
      <el-table-column label="名称">
        <template slot-scope="scope">
          {{ scope.row.plateName }}
        </template>
      </el-table-column>



      <el-table-column
        align="center"
        prop="created_at"
        label="创建时间"
        width="200"
      >
        <template slot-scope="scope">
          <i class="el-icon-time" />
          <span>{{ scope.row.gmtCreate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <i style="color: #409eff" class="el-icon-edit"></i>
          <el-button @click="edit(scope.row.plateId)" type="text"
          >修改</el-button
          >
          <i style="color: #409eff" class="el-icon-delete"></i>
          <el-button
            @click="open2(scope.row.plateId)"
            slot="reference"
            type="text"
          >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="params.pageNum"
        :page-sizes="[10, 20, 30, 50]"
        :page-size="limit"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import { listPlates } from "@/api/plate";
  import { deletePlate } from "@/api/plate";
  export default {
    filters: {
      statusFilter(status) {
        const statusMap = {
          published: "success",
          draft: "gray",
          deleted: "danger",
        };
        return statusMap[status];
      },
    },
    data() {
      return {
        list: null,
        total: 0,
        listLoading: true,
        params: {
          pageNum: 1,
          limit: 10,
        },

      };
    },
    created() {
      this.fetchData();
    },
    methods: {


      handleSizeChange(val) {
        console.log(val);
        this.params.limit = val;
        this.fetchData();
      },
      handleCurrentChange(val) {
        this.params.pageNum = val;
        this.fetchData();
      },
      fetchData() {
        this.listLoading = true;
        listPlates(this.params).then((res) => {
          console.log(res);
          this.listLoading = false;
          this.total=res.data.total;
          this.list = res.data.plateList;
          console.log(this.list);
        });
        // getList(this.listQuery).then(response => {
        // this.list = response.data.items
        // this.listLoading = false;
        // })
      },
      open2(plateId) {
        this.$confirm("是否删除?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            deletePlate(plateId)
              .then((res) => {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.fetchData();
              })
              .catch((reason) => {
                this.$message({
                  type: "error",
                  message: resason,
                });
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      },
      edit(plateId) {
        this.$router.push({
          path: "/plate/edit",
          query: {
            plateId: plateId,
          },
        });
      },
      toPlateAdd(){
        this.$router.push('/plate/save')
      }
    },
  };
</script>

<style  scoped>
  .grid-content {
    display: flex;
    align-items: center;
    justify-content: center;
  }
  .grid {
    font-weight: 600;
    color: #606266;
  }
  /deep/ .has-gutter th{
    background-color: #F8F8F9;
  }
</style>>

